package com.bprogram.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.bprogram.controller.UserFeign;
import com.bprogram.pojo.User;
import com.bprogram.pojo.Video;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Autowired
  private UserFeign userFeign;
    //新增默认字段默认值
    @Override
    public void insertFill(MetaObject metaObject) {
        User user = userFeign.getLoginUser();
        this.setFieldValByName("createId", user.getUserId(), metaObject);
        this.setFieldValByName("createCode", user.getUserCode(), metaObject);
        this.setFieldValByName("createName", user.getUserName(), metaObject);
        this.setFieldValByName("createTime", null, metaObject);


    }

    //修改时默认值
    @Override
    public void updateFill(MetaObject metaObject) {
        Video video = (Video) metaObject.getOriginalObject();
        User user =null;
        //判断是否为定时任务过程中进行了修改数据，若是，则不进行设置默认值，1738148946666666666L固定为定时任务发起人的userId
        if (!video.getUpdateId().equals(1738148946666666666L)){
            user = userFeign.getLoginUser();
            this.setFieldValByName("updateId", user.getUserId(), metaObject);
            this.setFieldValByName("updateCode", user.getUserCode(), metaObject);
            this.setFieldValByName("updateName", user.getUserName(), metaObject);
        }


    }

}